<?php
declare (strict_types = 1);

namespace app\api\controller\v1;

use app\api\model\v1\Admins;
use app\api\model\v1\Collect;
use app\api\model\v1\Community;
use app\api\model\v1\GatherCompany;
use app\api\model\v1\MonthReport;
use app\api\model\v1\Rectification;
use app\api\model\v1\Voluntary;
use app\BaseController;
use think\Request;

class Statistics extends BaseController
{


    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 包保单位采集统计
     */
    public function getGatherUnit(){
        $res = GatherCompany::field('id,gather_company_title')->withCount(['collect','status'=>function($query){
            $query->where(['status'=>Collect::IS_STATUS_ONE]);
            return $query;
        },'invalid'=>function($q_invalid){
            $q_invalid->where(['status'=>Collect::IS_STATUS_THREE]);
            return $q_invalid;
        }])->with(['community','recti'])->where(['type'=>GatherCompany::IS_TYPE_ONE])->order('collect_count desc')->select()->each(function ($item,$key){
            $zg_count = 0;
            if(!empty($item['recti'])){
                foreach ($item['recti'] as $k=>$v){
                    $zg_count += Rectification::where(['collect_id'=>$v['id'],'type'=>Rectification::IS_TYPE_TWO])->count();
                }
            }else{
                $zg_count = 0;
            }
            $item['zg_count'] = $zg_count;
        })->toArray();
        return $this->response($res);
    }


    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 日常采集单位统计表
     */
    public function getGatherUser(){
        $res = GatherCompany::field('id,gather_company_title')->withCount(['collect','status'=>function($query){
            $query->where(['status'=>Collect::IS_STATUS_ONE]);
            return $query;
        },'invalid'=>function($q_invalid){
            $q_invalid->where(['status'=>Collect::IS_STATUS_THREE]);
            return $q_invalid;
        }])->with(['recti'=>['admin']])->where(['type'=>GatherCompany::IS_TYPE_ONE])->order('collect_count desc')->select()->each(function ($item,$key){
            $zg_count = 0;
            $username = '';
            if(!empty($item['recti'])){
                foreach ($item['recti'] as $k=>$v){
                    $zg_count += Rectification::where(['collect_id'=>$v['id'],'type'=>Rectification::IS_TYPE_TWO])->count();
                    $username = $v['admin']['username'];
                }
            }else{
                $zg_count = 0;
                $username = '';
            }
            $item['zg_count'] = $zg_count;
            $item['username'] = $username;
        })->toArray();
        return $this->response($res);
    }


    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 志愿服务统计表
     */
    public function getVoluntaryCount(){
        $res = GatherCompany::field('id,gather_company_title')->withCount(['voluntary','vsatus'=>function($query){
            $query->where(['status'=>Voluntary::IS_STATUS_ONE]);
            return $query;
        },'notHg'=>function($query){
            $query->where(['status'=>Voluntary::IS_STATUS_THREE]);
            return $query;
        },'HGV'=>function($query){
            $query->where(['status'=>Voluntary::IS_STATUS_TWO]);
            return $query;
        }])->order('voluntary_count desc')->select()->toArray();
        return $this->response($res);
    }

    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 月报表统计
     */
    public function getMonthReportCount(){
        $res = GatherCompany::field('id,gather_company_title')->withCount(['monthReport','msatus'=>function($query){
            $query->where(['status'=>MonthReport::IS_STATUS_ONE]);
            return $query;
        },'notHgmr'=>function($query){
            $query->where(['status'=>MonthReport::IS_STATUS_THREE]);
            return $query;
        },'HGM'=>function($query){
            $query->where(['status'=>MonthReport::IS_STATUS_TWO]);
            return $query;
        }])->order('month_report_count desc')->select()->toArray();
        return $this->response($res);
    }

    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 单位整改统计表
     */
    public function getZgCount(){
        $res = GatherCompany::field('id,gather_company_title')->withCount(['rectification','yxZG'=>function($q){
            $q->where(['status'=>Rectification::IS_STATUS_TWO]);
            return $q;
        },'wxZG'=>function($wx){
            $wx->where(['status'=>Rectification::IS_STATUS_THREE]);
            return $wx;
        },'wZg'=>function($wzg){
            $wzg->where(['status'=>Rectification::IS_STATUS_ONE]);
            return $wzg;
        }])->where(['type'=>GatherCompany::IS_TYPE_TWO])->order('rectification_count desc')->select()->toArray();
        return $this->response($res);
    }

    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 个人整改统计
     */
    public function getGrCount(){
        $res = Admins::field('id,username')->withCount(['rectification','yxZG'=>function($q){
            $q->where(['status'=>Rectification::IS_STATUS_TWO]);
            return $q;
        },'wxZG'=>function($wx){
            $wx->where(['status'=>Rectification::IS_STATUS_THREE]);
            return $wx;
        },'wZg'=>function($wzg){
            $wzg->where(['status'=>Rectification::IS_STATUS_ONE]);
            return $wzg;
        }])->order('rectification_count desc')->select()->toArray();
        return $this->response($res);
    }
}
